﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TransFormer.Web.admin
{
    public partial class CompanyInfo : AdminPage
    {
        private BLL.CompanyInfo _cbll = new BLL.CompanyInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            var model = _cbll.GetModelList("language='english'").FirstOrDefault();
            ddlLanguage.Items.Add(new ListItem("英文", "english"));
            ddlLanguage.Items.Add(new ListItem("中文", "chinese"));
            if (model == null) return;
            ddlLanguage.Items.FindByValue("english");
            txtName.Text = model.Name;
            txtAddress.Text = model.Address;
            txtZip.Text = model.Zip;
            txtFax.Text = model.Fax;
            txtQQ.Text = model.QQ;
            txtAliWangWang.Text = model.AliWangWang;
            txtPhone.Text = model.Tel;
            txtPeople.Text = model.People;
            txtEmail.Text = model.Email;
            txtInfo.Text = model.Info;
            txtPlane.Text = model.Plane;
        }

        protected void DdlLanguageChanged(object sender, EventArgs e)
        {
            var language = ddlLanguage.SelectedValue;

            var model = _cbll.GetModelList("language='" + language + "'").FirstOrDefault();
            if (model == null)
            {
                txtName.Text = null;
                txtAddress.Text = null;
                txtZip.Text = null;
                txtFax.Text = null;
                txtQQ.Text = null;
                txtAliWangWang.Text = null;
                txtPhone.Text = null;
                txtPeople.Text = null;
                txtEmail.Text = null;
                txtInfo.Text = null;
                txtPlane.Text = null;
                return;
            }
            ddlLanguage.Items.FindByValue("english");
            txtName.Text = model.Name;
            txtAddress.Text = model.Address;
            txtZip.Text = model.Zip;
            txtFax.Text = model.Fax;
            txtQQ.Text = model.QQ;
            txtAliWangWang.Text = model.AliWangWang;
            txtPhone.Text = model.Tel;
            txtPeople.Text = model.People;
            txtEmail.Text = model.Email;
            txtInfo.Text = model.Info;
            txtPlane.Text = model.Plane;
        }

        protected void BtnOkClick(object sender, EventArgs e)
        {
            Model.CompanyInfo company = new Model.CompanyInfo();
            var language = ddlLanguage.SelectedValue;

            company.Name = txtName.Text;
            company.Address = txtAddress.Text;
            company.Zip = txtZip.Text;
            company.Fax = txtFax.Text;
            company.QQ = txtQQ.Text;
            company.AliWangWang = txtAliWangWang.Text;
            company.Tel = txtPhone.Text;
            company.People = txtPeople.Text;
            company.Email = txtEmail.Text;
            company.Info = txtInfo.Text;
            company.Language = language;
            company.Plane = txtPlane.Text;
            var model = _cbll.GetModelList("language='" + language + "'").FirstOrDefault();
            if(model==null)
            {
                _cbll.Add(company);
            }
            else
            {
                company.Id = model.Id;
                _cbll.Update(company);
            }


        }
    }
}